package com.lyd.algorithm;

import java.util.Arrays;

/**
 * @Author:lyd
 **/
public class IsomorphicTest {
    public static void main(String[] args) {

        int[] array = {3, 6, 8, 45, 78, 89, 99};
        int index = binarySearch(array, 45);
        System.out.println(index);
    }

    private static int isomorphic(String str1, String str2) {
        char[] array1 = str1.toCharArray();
        char[] array2 = str2.toCharArray();
        int[] hash = new int[26];
        for (int i = 0; i < hash.length; i++) {
            hash[i] = 0;
        }
        // hash数组记录array1每个字符出现的次数
        for (int i = 0; i < array1.length; i++) {
            hash[array1[i] - 'a']++;
        }
        // hash数组记录array2每个字符出现的次数
        //若对应字符在hash数组中为0，则array1和array2是同构字符串,否则则不是
        for (int i = 0; i < array2.length; i++) {
            hash[array2[i] - 'a']--;
        }
        for (int i = 0; i < hash.length; i++) {
            if (hash[i] != 0) {
                return -1;
            }
        }
        return 0;
    }

    public static int binarySearch(int[] array, int target) {
        int left = 0;
        int right = array.length - 1;
        while (left < right) {
            //避免溢出
            int mid = left + (right - left) / 2;
            if (array[mid] < target) {
                left = mid + 1;
            } else if (array[mid] > target) {
                right = mid;
            } else {
                return mid;
            }
        }
        return -1;
    }
}
